package com.hbisedm.interninfosys.listener;

import cn.hutool.extra.pinyin.PinyinUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.hbisedm.interninfosys.intern.entity.InternshipInfo;
import com.hbisedm.interninfosys.intern.entity.User;
import com.hbisedm.interninfosys.intern.entity.UserRole;
import com.hbisedm.interninfosys.intern.entity.vo.ExcelInternshipVo;
import com.hbisedm.interninfosys.intern.entity.vo.ExcelPersonVo;
import com.hbisedm.interninfosys.intern.service.IInternshipInfoService;
import com.hbisedm.interninfosys.intern.service.IUserRoleService;
import com.hbisedm.interninfosys.intern.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.ArrayList;
import java.util.List;

public class ExcelInternshipListener extends AnalysisEventListener<ExcelInternshipVo> {

    private BCryptPasswordEncoder bCryptPasswordEncoder;
    private IUserService userService;
    private IInternshipInfoService internshipInfoService;
    private IUserRoleService userRoleService;


    List<ExcelInternshipVo> list = new ArrayList<>();

    public ExcelInternshipListener(BCryptPasswordEncoder bCryptPasswordEncoder, IUserService userService, IUserRoleService userRoleService, IInternshipInfoService internshipInfoService) {
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
        this.userService = userService;
        this.internshipInfoService = internshipInfoService;
        this.userRoleService = userRoleService;
    }

    @Override
    public void invoke(ExcelInternshipVo excelInternshipVo, AnalysisContext analysisContext) {
        list.add(excelInternshipVo);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        saveData();
    }


    /**
     * 加上存储数据库
     */
    private void saveData() {
        ArrayList<User> users = new ArrayList<>();
        ArrayList<InternshipInfo> internshipInfos = new ArrayList<>();
        list.stream().forEach(item -> {
//            String pinyin = PinyinUtil.getPinyin(item.getName());
            User user = new User();
            BeanUtils.copyProperties(item, user);
//            String pinyin = PinyinUtil.getPinyin(item.getOthername());

            user.setPassword(bCryptPasswordEncoder.encode("123456"));
            /**
             * 设置为企业
             */
            user.setType(3);

            /**
             * 持久化user后可以拿id
             */
            userService.save(user);

            InternshipInfo internshipInfo = new InternshipInfo();
            internshipInfo.setInternshipIntroduce(item.getInternshipIntroduce());
            internshipInfo.setInternshipName(item.getOthername());
            internshipInfo.setLocationInfo(item.getLocationInfo());
            internshipInfo.setUid(user.getId());
            //设置普通角色
            UserRole userRole = new UserRole(user.getId(), 2);
            userRoleService.save(userRole);

            internshipInfos.add(internshipInfo);
        });

        internshipInfoService.saveBatch(internshipInfos);
        System.out.println("存储数据库成功！");
    }

}
